using System;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Configuration;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Threading;
using System.Xml;
using Flitter.Properties;

namespace Flitter.Twitter
{
    public class DataModel : INotifyPropertyChanged
    {

        public DataModel()
        {
            _dispatcher = Dispatcher.CurrentDispatcher;
        }

        public enum ModelState
        {
            /// <summary>
            /// The model is fetching data
            /// </summary>
            Fetching,
            /// <summary>
            /// The model is in an invalid state
            /// </summary>
            Invalid,
            /// <summary>
            /// The model has fetched its data
            /// </summary>
            Active
        }

        public ModelState State
        {
            get
            {
                VerifyCalledOnUIThread();
                return _state;
            }

            set
            {

                VerifyCalledOnUIThread();
                if (value != _state)
                {
                    _state = value;
                    SendPropertyChanged("State");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged
        {
            add
            {

                VerifyCalledOnUIThread();
                _propertyChangedEvent += value;
            }

            remove
            {
                VerifyCalledOnUIThread();
                _propertyChangedEvent -= value;
            }
        }

        protected void SendPropertyChanged(string propertyName)
        {

            VerifyCalledOnUIThread();
            if (_propertyChangedEvent != null)
            {
                _propertyChangedEvent(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private ModelState _state;
        private Dispatcher _dispatcher;
        public Dispatcher Dispatcher
        {
            get { return _dispatcher; }
        }

        private PropertyChangedEventHandler _propertyChangedEvent;
        [Conditional("Debug")]
        protected void VerifyCalledOnUIThread()
        {
            Debug.Assert(Dispatcher.CurrentDispatcher == this.Dispatcher,
                "Call must be made on UI thread.");
        }

        private bool _isActive;
        public bool IsActive
        {
            get
            {
                VerifyCalledOnUIThread();
                return _isActive;
            }

            private set
            {
                VerifyCalledOnUIThread();
                if (value != _isActive)
                {
                    _isActive = value;
                    SendPropertyChanged("IsActive");
                }
            }
        }

        public void Activate()
        {
            VerifyCalledOnUIThread();

            if (!_isActive)
            {
                this.IsActive = true;
                OnActivated();
            }
        }
        public void Deactivate()
        {
            VerifyCalledOnUIThread();

            if (_isActive)
            {
                this.IsActive = false;
                OnDeactivated();
            }
        }

        protected virtual void OnActivated()
        {
        }
        protected virtual void OnDeactivated()
        {
        }

    }
}
